package com.yz.test;

import java.util.Scanner;
//统计字母出现的次数
public class CountEachLetter {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入你要输入的字符串：");
        String s = input.nextLine();
        int[] ints = countLetters(s.toLowerCase());
        for (int i = 0; i < ints.length; i++) {
            if (ints[i] != 0){
                System.out.println((char)('a'+i) + "出现了"+ints[i] +
                        ((ints[i] == 1) ? " time" : " times"));
            }
        }
    }
    //统计字母出现的次数
    public static int[] countLetters(String s){
        int[] counts = new int[24];
        for (int i = 0; i < s.length(); i++) {
            //如果这个字符串都是对应一个字符，那么就返回true
            if (Character.isLetter(s.charAt(i))){
                counts[s.charAt(i) - 'a']++;
            }
        }
        return counts;
    }
}
